package com.edmond.controller;

import com.edmond.entity.User;
import com.edmond.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserHandler {
    @Value("${server.port}")
    private String port;

    @Autowired
    private UserRepository userRepository;

    //  查询所有
    @GetMapping("/findAll")
    public List<User> findAll(@RequestParam("index") Integer index, @RequestParam("pageSize") Integer pageSize) {
        return userRepository.findAll(index, pageSize);
    }

    //  根据 id 进行查询
    @GetMapping("/findById/{id}")
    public User findById(@PathVariable("id") Integer id) {
        return userRepository.findById(id);
    }

    //  查询总数
    @GetMapping("/count")
    public int count() {
        return userRepository.count();
    }

    //  新增对象信息
    @PostMapping("/save")
    public void save(@RequestBody User user) {
        userRepository.save(user);
    }

    //  修改用户信息
    @PutMapping("/update")
    public void update(@RequestBody User user) {
        userRepository.update(user);
    }

    //  根据id删除
    @DeleteMapping("/deleteById/{id}")
    public void deleteById(@PathVariable("id") Integer id) {
        userRepository.deleteById(id);
    }
}
